<?php


namespace app\agent\controller\coupon;

use app\agent\controller\Controller;
use app\model\Agent;
use app\model\CouponBrand;
use app\model\CouponConfig;
use app\Request;
use app\agent\validate\coupon\Premium as PremiumValidate;
use app\model\CouponProductChannel;

class Premium extends Controller
{
    protected function initialize()
    {
        parent::initialize();
    }

    /**
     * 溢价管理
     * @access public
     * @method post
     * @return array
     */
    public function setPremium(Request $request, PremiumValidate $validate)
    {
        $params = $request->param();
        //是否修改
        $id = $request->param('id');
        $type = $request->param('type');
        $config_type = $request->param('config_type')??0;
        $global = $request->param('global')??0;
        $agent_id = $request->param('agent_id')??0;
        $quote_id = $request->param('quote_id')??0;
        $premium_rate = $request->param('premium_rate');
        $status = $request->param('status')??1;
        $result = $validate->scene('common')->check($params);
        if(!$result) {
            return fail($validate->getError());
        }
        if ($type == CouponConfig::PREMIUM_TYPE_QJ) { //全局溢价类型
            $result = $validate->scene('global')->check($params);
            if(!$result) {
                return fail($validate->getError());
            }
            if ($global == CouponConfig::GLOBAL_PTDLQJ || $global == CouponConfig::GLOBAL_DLQJ) {
                if (empty($agent_id)) {
                    return fail('为单个代理或者代理本身设置全局溢价，代理id不能为空');
                }
            }
        } else { //单个商品类型溢价设置
            $result = $validate->scene('config_set')->check($params);
            if(!$result) {
                return fail($validate->getError());
            }
            if ($config_type == CouponConfig::CONFIG_TYPE_PTDLDG || $config_type == CouponConfig::CONFIG_TYPE_DLDG) {
                if (empty($agent_id)) {
                    return fail('为单个代理或者代理本身设置全局溢价，代理id不能为空');
                }
            }
            if (empty($quote_id)) {
                return fail('为单个代理或者代理本身设置全局溢价，商品id不能为空');
            }
        }
        //验证代理
        if (!empty($agent_id)) {
            $agent = \app\model\Agent::where('id', $agent_id)->findOrEmpty();
            if ($agent->isEmpty()) {
                return fail('代理不存在');
            }
            //代理禁用......
            if ($agent->status == Agent::STATUS_CLOSE) {
                return fail('代理已被禁用');
            }
        }
        //验证商品
        if (!empty($quote_id)) {
            $productChannel = CouponProductChannel::where('id', $quote_id)->where('status', CouponProductChannel::STATUS_ACTIVE)->findOrEmpty();
            if ($productChannel->isEmpty()) {
                return fail('设置失败，商品不存在');
            }
            if ($productChannel->getData('shelf_state') != CouponProductChannel::SHELF_STATE_SZ) {
                return fail('设置失败，商品未上架');
            }
        }
        $info = CouponConfig::where('id', $id)->findOrEmpty();
        if (!$info->isEmpty()) { //修改
            $info->agent_id = $agent_id;
            $info->quote_id = isset($productChannel->product_id)?$productChannel->product_id:0;
            $info->config_type = $config_type;
            $info->global = $global;
            $info->status = $status;
            if ($status == 1) {
                $info->value = $premium_rate;
            }
            if (!$info->save()) {
                return fail('溢价管理设置，修改失败');
            }
            return success('溢价管理设置成功');
        }
        $config = CouponConfig::findOrCreate([
            'agent_id'    => $agent_id,
            'quote_id'    => isset($productChannel->product_id)?$productChannel->product_id:0,
            'config_type' => $config_type,
            'global'      => $global,
            'status'      => $status,
        ],[
            'value'       => $premium_rate
        ]);
        if ($config->id) {
            return success('溢价管理设置成功');
        }
        return fail('溢价管理设置失败');
    }

    /**
     * 溢价管理----溢价管理列表
     * @access public
     * @method get
     * @return array
     */
    public function defaultTemp(Request $request)
    {
        $params = input();
        $params['config_type'] = CouponConfig::CONFIG_TYPE_DLDG;
        $params['is_agent'] = 1;
        $list = (new CouponBrand())->getDefaultTempList($params);
        //查询全局溢价设置
        $map = [
            'global'    =>  CouponConfig::GLOBAL_DLQJ,
            'agent_id'  => $params['agent_id'],
        ];
        $config = (new CouponConfig())->findConfigByParams($map);
        $list['global_config'] = $config;
        return success('请求成功', $list);
    }
}